package com.ly.hotel.view.floatWindows;

import java.lang.ref.WeakReference;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

import com.ly.hotel.adaptor.HardwareInterface;
import com.ly.hotel.common.SysConf;
import com.ly.hotel.util.DensityUtil;

public class ShuaKaFloatWin extends Service {
	// 定义浮动窗口布局
//	LinearLayout mFloatLayout;
	private LayoutParams wmParams;
	// 创建浮动窗口设置布局参数的对象
	private WindowManager mWindowManager;

//	ImageButton mFloatView;
	private static ThisHandler handler;
	private ShuaKafloatView gf;
	private Context context;
	@Override
	public void onCreate() {
		super.onCreate();
		context = getBaseContext();
		
		handler = new ThisHandler(this);
		createFloatView();
		
//		registerReceiver(bottomMenuRev, new IntentFilter(Constants.Broadcast.SHUAKA_TIPS));
		
//		sendBroadcast(new Intent(Constants.Broadcast.SHUAKA_TIPS).putExtra("flag", Constants.CLOSE_TIPS));
	}


	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	private void createFloatView() {
		wmParams = new LayoutParams();
		getApplication();
		// 获取WindowManagerImpl.CompatModeWrapper
		mWindowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);

		DisplayMetrics dm = new DisplayMetrics();
		mWindowManager.getDefaultDisplay().getMetrics(dm);

		// 设置window type
		wmParams.type = 2003;
		// 设置图片格式，效果为背景透明
		wmParams.format = PixelFormat.RGBA_8888;
		// 设置浮动窗口不可聚焦（实现操作除浮动窗口外的其他可见窗口的操作）
		wmParams.flags = 
		// LayoutParams.FLAG_NOT_TOUCH_MODAL |
		LayoutParams.FLAG_NOT_FOCUSABLE
		// LayoutParams.FLAG_NOT_TOUCHABLE
		;

		// 调整悬浮窗显示的停靠位置为左侧置顶
		wmParams.gravity = Gravity.CENTER | Gravity.BOTTOM;
 
		// 设置悬浮窗口长宽数据
		wmParams.width = DensityUtil.dip2px(250);
		wmParams.height = DensityUtil.dip2px(250); 

		LayoutInflater inflater = LayoutInflater.from(getApplication());
		// 获取浮动窗口视图所在布局 

		gf = new ShuaKafloatView(context);
		
		gf.setVisibility(View.INVISIBLE);
		
		mWindowManager.addView(gf, wmParams);

	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		HardwareInterface.setCardLed(context, false);
		gf.setVisibility(View.INVISIBLE);
		super.onDestroy();
	}

	public static void showShuakaWin(){
		handler.sendEmptyMessage(1);
	}
	
	public static void hideShuakaWin(){
		handler.sendEmptyMessage(2);
	}
	 
	private static class ThisHandler extends Handler {
		private WeakReference<ShuaKaFloatWin> wrActivity;

		public ThisHandler(ShuaKaFloatWin activity) {
			wrActivity = new WeakReference<ShuaKaFloatWin>(activity);
		}

		@Override
		public void handleMessage(Message message) {
			ShuaKaFloatWin act = wrActivity.get();
			if (act != null) {
				// 这个是通过事实消息接口获取的弹窗信息打开的
				if (message.what == 0) {
					act.stopSelf();
				} else if (message.what == 1) {
					act.gf.setVisibility(View.VISIBLE);
					
					if(handler.hasMessages(2)){
						handler.removeMessages(2);
					}
					//设置定时隐藏
					sendEmptyMessageDelayed(2, Integer.parseInt(SysConf.extendConfGetValue(SysConf.SYS_CONF_SHUAKA_TIPS_SHOW_TIME,"4000")));
					
				} else if (message.what == 2) {
					act.gf.setVisibility(View.INVISIBLE);
				}
			}
		}
	}

}
